package com.chw.admin.sysiconinfo;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chw.utils.CommonFieldsUtil;
import com.chw.utils.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class SysIconInfoService implements ISysIconInfoService {
	@Autowired
	private SysIconInfoMapper sysIconInfoMapper;
	@Autowired
	private ISysUploadFilesService sysUploadFilesService;
	
	@Override
	public PageInfo<SysIconInfo> find(SysIconInfo sysIconInfo, Integer currentPage, Integer pageSize) {
		PageHelper.startPage(currentPage, pageSize);
		List<SysIconInfo> list =sysIconInfoMapper.find(sysIconInfo);
		if(list == null || list.size()==0){
			list = new ArrayList<SysIconInfo>();
		}
		return new PageInfo<SysIconInfo>(list);
	}
	
	@Override
	public List<SysIconInfo> findByIconShowType(String iconShowType) {
		if(iconShowType != null && iconShowType.length()>0){
			SysIconInfo sysIconInfo = new SysIconInfo();
			sysIconInfo.setIconShowType(iconShowType);
			List<SysIconInfo> list =sysIconInfoMapper.find(sysIconInfo);
			return list;
		}else{
			return new ArrayList<SysIconInfo>();
		}
	}

	@Override
	public int delSysIconInfo(String id) {
		try {
			SysIconInfo sysIconInfo=sysIconInfoMapper.selectByPrimaryKey(id);
			sysIconInfo.setEnabledFlag("N");
			sysIconInfoMapper.updateByPrimaryKey(sysIconInfo);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int save(SysIconInfo sysIconInfo) {
		try {
			//先把同类型的图标先软删、禁用   然后再新增
			if(sysIconInfo.getIconShowType() != null && sysIconInfo.getIconShowType().length()>0){
				if(sysIconInfo.getIconStatus() != null && sysIconInfo.getIconStatus().equals("Y")){
					SysIconInfo sysIconInfoTemp = new SysIconInfo();
					sysIconInfoTemp.setIconShowType(sysIconInfo.getIconShowType());
					sysIconInfoTemp.setIconStatus("N");
					sysIconInfoTemp.setLastUpdatedBy(CommonFieldsUtil.getCurrentUserId());
					sysIconInfoTemp.setLastUpdatedDate(new Date());
					sysIconInfoMapper.updateStatusAndEnabled(sysIconInfoTemp);
				}
			}
			String iconInfoId = UUIDUtil.getUUID();
			sysIconInfo.setIconInfoId(iconInfoId);
			sysIconInfo.setCreatedBy(CommonFieldsUtil.getCurrentUserId());
			sysIconInfo.setCreatedDate(new Date());
			sysIconInfo.setLastUpdatedBy(CommonFieldsUtil.getCurrentUserId());
			sysIconInfo.setLastUpdatedDate(new Date());
			sysIconInfo.setEnabledFlag("Y");
			//sysIconInfo.setOrgId(CommonFieldsUtil.getCurrentOrgId());
			sysIconInfoMapper.insert(sysIconInfo);
			String pictureUrl = sysIconInfo.getPictureUrl();
			if(pictureUrl != null && pictureUrl.length()>0){
				String[] psrc = pictureUrl.split(",");
				
				String fileSizeStr = sysIconInfo.getFileSize();
				String fileTypeStr = sysIconInfo.getFileType();
				String upFileNameStr = sysIconInfo.getUpFileName();
				String[] fileSizes = fileSizeStr.split(",");
				String[] fileTypes = fileTypeStr.split(",");
				String[] upFileNames = upFileNameStr.split(",");
				
				List<SysUploadFiles> ulfs = new ArrayList<SysUploadFiles>();
				for(int i=0;i<psrc.length;i++){
					String src = psrc[i];
					String fileSize = fileSizes[i];
					String fileType = fileTypes[i];
					String upFileName = upFileNames[i];
					SysUploadFiles sysUploadFiles = new SysUploadFiles();
					sysUploadFiles.setCreatedBy(CommonFieldsUtil.getCurrentUserId());
					sysUploadFiles.setCreatedDate(new Date());
					sysUploadFiles.setLastUpdatedBy(CommonFieldsUtil.getCurrentUserId());
					sysUploadFiles.setLastUpdatedDate(new Date());
					sysUploadFiles.setEnabledFlag("Y");
					sysUploadFiles.setRefId(iconInfoId);
					sysUploadFiles.setUpaddr(src);
					sysUploadFiles.setFilesId(UUIDUtil.getUUID());
					sysUploadFiles.setUpfilename(upFileName);
					sysUploadFiles.setFilesize(fileSize);
					sysUploadFiles.setFiletype(fileType);
					ulfs.add(sysUploadFiles);
				}
				sysUploadFilesService.saveList(ulfs);
				/*List<SysUploadFiles> sysUploadFilesList = sysUploadFilesService.findByIds(filesIds);
				if(sysUploadFilesList != null && sysUploadFilesList.size()>0){
					List<SysUploadFiles> sysUploadFilesList_temp = new ArrayList<SysUploadFiles>();
					for(SysUploadFiles sysUploadFiles : sysUploadFilesList){
						sysUploadFiles.setRefId(iconInfoId);
						sysUploadFiles.setLastUpdatedBy(CommonFieldsUtil.getCurrentUserId());
						sysUploadFiles.setLastUpdatedDate(new Date());
						sysUploadFilesList_temp.add(sysUploadFiles);
					}
					sysUploadFilesService.bathUpdateUploadFiles(sysUploadFilesList_temp);
				}*/
			}
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public SysIconInfo findById(String id) {
		SysIconInfo sysIconInfo=sysIconInfoMapper.selectByPrimaryKey(id);
		List<SysUploadFiles> list = sysUploadFilesService.findByRefId(id);
		if(list != null && list.size()>0){
			StringBuffer sb = new StringBuffer();
			StringBuffer sbIds = new StringBuffer();
			for(SysUploadFiles suf : list){
				sb.append(",");
				sb.append(suf.getUpaddr());
				sbIds.append(",");
				sbIds.append(suf.getFilesId());
			}
			String temp = sb.toString();
			temp = temp.substring(1);
			sysIconInfo.setPictureUrl(temp);
			
			String sbIdstTemp = sbIds.toString();
			sbIdstTemp = sbIdstTemp.substring(1);
			sysIconInfo.setFilesIds(sbIdstTemp);
		}
		return sysIconInfo;
	}

	@Override
	public int update(SysIconInfo sysIconInfo) {
		try {
			sysIconInfo.setLastUpdatedDate(new Date());
			sysIconInfo.setLastUpdatedBy(CommonFieldsUtil.getCurrentUserId());
			sysIconInfoMapper.updateByPrimaryKeySelective(sysIconInfo);
			String filesIds = sysIconInfo.getFilesIds();
			List<SysUploadFiles> sysUploadFilesListDb = sysUploadFilesService.findByRefId(sysIconInfo.getIconInfoId());
			if(filesIds != null && filesIds.length()>0){
				List<SysUploadFiles> sysUploadFilesList = sysUploadFilesService.findByIds(filesIds);
				if(sysUploadFilesList != null && sysUploadFilesList.size()>0){
					String pictureUrl = sysIconInfo.getPictureUrl();
					String[] psrc = pictureUrl.split(",");
					String fileSizeStr = sysIconInfo.getFileSize();
					String fileTypeStr = sysIconInfo.getFileType();
					String upFileNameStr = sysIconInfo.getUpFileName();
					String[] fileSizes = fileSizeStr.split(",");
					String[] fileTypes = fileTypeStr.split(",");
					String[] upFileNames = upFileNameStr.split(",");
					Map<String,SysUploadFiles> dbSysUploadFilesMap = new HashMap<String,SysUploadFiles>();
					for(SysUploadFiles sysUploadFiles : sysUploadFilesList){
						dbSysUploadFilesMap.put(sysUploadFiles.getFilesId(), sysUploadFiles);
					}
					List<SysUploadFiles> sysUploadFilesList_temp = new ArrayList<SysUploadFiles>();
					List<SysUploadFiles> sysUploadFilesListInsert_temp = new ArrayList<SysUploadFiles>();
					for(int i=0;i<sysUploadFilesList.size();i++){
						SysUploadFiles sysUploadFiles = sysUploadFilesList.get(i);
						if(!dbSysUploadFilesMap.containsKey(sysUploadFiles.getFilesId())){
							String thisPic = psrc[i];
							String thisFs = fileSizes[i];
							String thisFt = fileTypes[i];
							String thisFn = upFileNames[i];
							SysUploadFiles newSysUploadFiles = new SysUploadFiles();
							newSysUploadFiles.setCreatedBy(CommonFieldsUtil.getCurrentUserId());
							newSysUploadFiles.setCreatedDate(new Date());
							newSysUploadFiles.setLastUpdatedBy(CommonFieldsUtil.getCurrentUserId());
							newSysUploadFiles.setLastUpdatedDate(new Date());
							newSysUploadFiles.setEnabledFlag("Y");
							newSysUploadFiles.setRefId(sysIconInfo.getIconInfoId());
							newSysUploadFiles.setUpaddr(thisPic);
							newSysUploadFiles.setFilesId(UUIDUtil.getUUID());
							newSysUploadFiles.setUpfilename(thisFn);
							newSysUploadFiles.setFilesize(thisFs);
							newSysUploadFiles.setFiletype(thisFt);
							sysUploadFilesListInsert_temp.add(newSysUploadFiles);
						}else{
							sysUploadFiles.setRefId(sysIconInfo.getIconInfoId());
							sysUploadFiles.setLastUpdatedBy(CommonFieldsUtil.getCurrentUserId());
							sysUploadFiles.setLastUpdatedDate(new Date());
							sysUploadFiles.setEnabledFlag("N");
							sysUploadFilesList_temp.add(sysUploadFiles);
						}
					}
					sysUploadFilesService.bathUpdateUploadFiles(sysUploadFilesList_temp);
					sysUploadFilesService.saveList(sysUploadFilesList_temp);
				}else{
					if(sysUploadFilesListDb != null && sysUploadFilesListDb.size()>0){
						List<SysUploadFiles> sysUploadFilesList_temp = new ArrayList<SysUploadFiles>();
						for(int i=0;i<sysUploadFilesListDb.size();i++){
							SysUploadFiles sysUploadFiles = sysUploadFilesListDb.get(i);
							sysUploadFiles.setRefId(sysIconInfo.getIconInfoId());
							sysUploadFiles.setLastUpdatedBy(CommonFieldsUtil.getCurrentUserId());
							sysUploadFiles.setLastUpdatedDate(new Date());
							sysUploadFiles.setEnabledFlag("N");
							sysUploadFilesList_temp.add(sysUploadFiles);
						}
						sysUploadFilesService.bathUpdateUploadFiles(sysUploadFilesList_temp);
						List<SysUploadFiles> sysUploadFilesListInsert_temp = new ArrayList<SysUploadFiles>();
						String pictureUrl = sysIconInfo.getPictureUrl();
						String[] psrc = pictureUrl.split(",");
						String fileSizeStr = sysIconInfo.getFileSize();
						String fileTypeStr = sysIconInfo.getFileType();
						String upFileNameStr = sysIconInfo.getUpFileName();
						String[] fileSizes = fileSizeStr.split(",");
						String[] fileTypes = fileTypeStr.split(",");
						String[] upFileNames = upFileNameStr.split(",");
						for(int i=0;i<psrc.length;i++){
							String thisPic = psrc[i];
							String thisFs = fileSizes[i];
							String thisFt = fileTypes[i];
							String thisFn = upFileNames[i];
							SysUploadFiles newSysUploadFiles = new SysUploadFiles();
							newSysUploadFiles.setCreatedBy(CommonFieldsUtil.getCurrentUserId());
							newSysUploadFiles.setCreatedDate(new Date());
							newSysUploadFiles.setLastUpdatedBy(CommonFieldsUtil.getCurrentUserId());
							newSysUploadFiles.setLastUpdatedDate(new Date());
							newSysUploadFiles.setEnabledFlag("Y");
							newSysUploadFiles.setRefId(sysIconInfo.getIconInfoId());
							newSysUploadFiles.setUpaddr(thisPic);
							newSysUploadFiles.setFilesId(UUIDUtil.getUUID());
							newSysUploadFiles.setUpfilename(thisFn);
							newSysUploadFiles.setFilesize(thisFs);
							newSysUploadFiles.setFiletype(thisFt);
							sysUploadFilesListInsert_temp.add(newSysUploadFiles);
						}
						sysUploadFilesService.saveList(sysUploadFilesListInsert_temp);
					}else{
						List<SysUploadFiles> sysUploadFilesListInsert_temp = new ArrayList<SysUploadFiles>();
						String pictureUrl = sysIconInfo.getPictureUrl();
						String[] psrc = pictureUrl.split(",");
						String fileSizeStr = sysIconInfo.getFileSize();
						String fileTypeStr = sysIconInfo.getFileType();
						String upFileNameStr = sysIconInfo.getUpFileName();
						String[] fileSizes = fileSizeStr.split(",");
						String[] fileTypes = fileTypeStr.split(",");
						String[] upFileNames = upFileNameStr.split(",");
						for(int i=0;i<psrc.length;i++){
							String thisPic = psrc[i];
							String thisFs = fileSizes[i];
							String thisFt = fileTypes[i];
							String thisFn = upFileNames[i];
							SysUploadFiles newSysUploadFiles = new SysUploadFiles();
							newSysUploadFiles.setCreatedBy(CommonFieldsUtil.getCurrentUserId());
							newSysUploadFiles.setCreatedDate(new Date());
							newSysUploadFiles.setLastUpdatedBy(CommonFieldsUtil.getCurrentUserId());
							newSysUploadFiles.setLastUpdatedDate(new Date());
							newSysUploadFiles.setEnabledFlag("Y");
							newSysUploadFiles.setRefId(sysIconInfo.getIconInfoId());
							newSysUploadFiles.setUpaddr(thisPic);
							newSysUploadFiles.setFilesId(UUIDUtil.getUUID());
							newSysUploadFiles.setUpfilename(thisFn);
							newSysUploadFiles.setFilesize(thisFs);
							newSysUploadFiles.setFiletype(thisFt);
							sysUploadFilesListInsert_temp.add(newSysUploadFiles);
						}
						sysUploadFilesService.saveList(sysUploadFilesListInsert_temp);
					}
				}
			}else{
				if(sysUploadFilesListDb != null && sysUploadFilesListDb.size()>0){
					List<SysUploadFiles> sysUploadFilesList_temp = new ArrayList<SysUploadFiles>();
					for(int i=0;i<sysUploadFilesListDb.size();i++){
						SysUploadFiles sysUploadFiles = sysUploadFilesListDb.get(i);
						sysUploadFiles.setRefId(sysIconInfo.getIconInfoId());
						sysUploadFiles.setLastUpdatedBy(CommonFieldsUtil.getCurrentUserId());
						sysUploadFiles.setLastUpdatedDate(new Date());
						sysUploadFiles.setEnabledFlag("N");
						sysUploadFilesList_temp.add(sysUploadFiles);
					}
					sysUploadFilesService.bathUpdateUploadFiles(sysUploadFilesList_temp);
				}
			}
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	@Override
	public List<SysIconInfo> findIconShow(SysIconInfo record){
		record.setEnabledFlag("Y");
		List<SysIconInfo> list = sysIconInfoMapper.findIconShow(record);
		List<SysIconInfo> list_temp = new ArrayList<SysIconInfo>();
		if(list != null && list.size()>0){
			for(SysIconInfo sii : list){
				if(sii.getPictureUrl() != null && sii.getPictureUrl().length()>0){
					list_temp.add(sii);
				}
			}
		}
		return list_temp;
	}

	@Override
	public int setStatus(String sysIconInfoId, String iconStatus,String iconShowType) {
		SysIconInfo sysIconInfoTemp = new SysIconInfo();
		sysIconInfoTemp.setIconShowType(iconShowType);
		sysIconInfoTemp.setIconStatus("N");
		sysIconInfoTemp.setLastUpdatedBy(CommonFieldsUtil.getCurrentUserId());
		sysIconInfoTemp.setLastUpdatedDate(new Date());
		sysIconInfoMapper.updateStatusAndEnabled(sysIconInfoTemp);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sysIconInfoId", sysIconInfoId);
		map.put("iconStatus", iconStatus);
		try {
			sysIconInfoMapper.updateStatus(map);
			
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

}
